<template>
	<!-- pages/chat/chat.wxml -->
	<view class="container">
		<!-- 消息列表区域 -->
		<scroll-view class="message-container" :scroll-y="true" :scroll-into-view="scrollToMessageId" id="container">
			<!-- 消息列表 -->
			<view class="message-list" id="list">
				<view v-for="(item,index) in messageList" :key="index" class="message-item" :id="item.msgid">
					<view class="message-left" v-if="item.who === 'friend'">
						<image class="avatar" :src="friendsheaderimg"></image>
						<view class="content">
							<text class="text">{{item.msgtxt}}</text>
						</view>
					</view>
					<view v-else class="message-right">
						<view class="content">
							<text class="text">{{item.msgtxt}}</text>
						</view>
						<image class="avatar" :src="myheaderimg"></image>
					</view>
				</view>
			</view>
			<view class="message-bottom" id="bottom">
				<view class="box">
					<uv-icon @click="setShow=true" name="setting-fill" color="#999" size="22"></uv-icon>
					<input type="text" v-model="inputValue" @confirm="sendMessage" />
					<view class="button" @click="sendMessage">发送</view>
				</view>
			</view>
		</scroll-view>
		<view class="setBox" v-if="setShow">
			<view class="bj" @click="setShow = false"></view>
			<view class="box">
				<view class="topList">
					<view class="list" @click="link('/comm/card/card?userid='+id)">查看名片</view>
					<view class="list" @click="link('/user/feedback/feedback')">投诉</view>
					<view class="list" @click="dele">删除聊天记录</view>
				</view>
				<view class="quxiao" @click="setShow = false">取消</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {link} from '@/utils/utils.js'
	import {
		ref,
		getCurrentInstance,
		nextTick
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {
		onLoad,
		onShow,
		onUnload,
	} from '@dcloudio/uni-app'
	const _this = getCurrentInstance()
	const windowHeight = ref(0)
	const messageList = ref([])
	onLoad((open) => {
		id.value = open.id
		getData()
		time()
	})
	onUnload(() => {
		if(setTime.value){
			clearInterval(setTime.value)
		}
	})
	
	const id = ref('')
	const setTime = ref(null)
	const time = ()=>{
		setTime.value = setInterval(()=>{
			getData()
		},6500)
	}
	const scrollToMessageId = ref('')
	const friendsheaderimg = ref('')
	const myheaderimg = ref('')
	const chatmsg_lastid = ref('')
	const getData = async()=>{
		let data = {
			do:'txlmsgfriend',
			fid:id.value
		}
		let res = await homeApi.publicGet(data)
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'txlmsgfriend') {
				messageList.value = res.columns[i].chatmsg?res.columns[i].chatmsg:[]
				friendsheaderimg.value = res.columns[i].friendsheaderimg?res.columns[i].friendsheaderimg:''
				myheaderimg.value = res.columns[i].myheaderimg?res.columns[i].myheaderimg:''
				chatmsg_lastid.value = res.columns[i].chatmsg_lastid?res.columns[i].chatmsg_lastid:''
			}
		}
		let index = parseInt(messageList.value.length)-1
		nextTick(()=>{
			scrollToMessageId.value = 'bottom'
		})
	}
	const inputValue = ref('')
	const sendMessage = async()=>{
		let data = {
			lastid:chatmsg_lastid.value,
			newmsg:inputValue.value
		}
		let res = await homeApi.publicPost(data,`txlmsgfriendsendchatmsg&fid=${id.value}`)
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'txlmsgfriendsendchatmsg'){
				if(res.columns[i].statuscode=='200'){
					getData()
					inputValue.value=''
				}else{
					uni.showToast({
						title: res.columns[i].message,
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
	}
	const setShow = ref(false)
	
	const dele = ()=>{
		uni.showModal({
			title: "提示",
			content: "确认要删除聊天记录吗？",
			success(res) {
				if(res.confirm){
					deleLink()
				}
			},
		});
	}
	const deleLink = async()=>{
		let data = {
			DelChatLog:'Yes'
		}
		let res = await homeApi.publicPost(data,`txlmsgfrienddelchatlog&fid=${id.value}`)
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == `txlmsgfrienddelchatlog`) {
				if(res.columns[i].statuscode=='200'){
					uni.showToast({
						title: '操作成功',
						icon: 'none',
						duration: 2000
					});
					getData()
				}else{
					uni.showToast({
						title: res.columns[i].message,
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
		setShow.value = false
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}

	.message-container {
		flex: 1;
		height: 100%;
		background-color: #ebecf5;
	}

	.message-list {
		display: flex;
		flex-direction: column;
		padding: 30rpx;
		flex: 1;
		padding-bottom: 140rpx;
	}

	.message-item {
		margin-bottom: 15px;
	}

	.message-left,
	.message-right {
		display: flex;
		align-items: flex-start;
		margin-bottom: 10px;
	}

	.message-left {
		justify-content: flex-start;
	}

	.message-right {
		justify-content: flex-end;
	}

	.avatar {
		width: 40px;
		height: 40px;
		border-radius: 50%;
		margin: 0 10px;
	}

	.content {
		max-width: 70%;
		padding: 10px;
		border-radius: 8px;
		position: relative;
	}

	.message-left .content {
		background-color: #fff;
	}

	.message-right .content {
		background-color: #95ec69;
	}

	.text {
		font-size: 16px;
	}

	.message-bottom {
		width: 100%;
		height: 140rpx;
		position: fixed;
		bottom: 0;
		left: 0;

		.box {
			width: calc(100% - 60rpx);
			height: 80rpx;
			background: #f0f3fc;
			padding: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			input {
				flex: 1;
				border-radius: 30px;
				background: #fff;
				padding: 20rpx;
				font-size: 30rpx;
				margin-left: 30rpx;
				margin-right: 30rpx;
			}

			.button {
				width: 80rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				color: #333;
				border-radius: 8px;
				font-size: 32rpx;
			}
		}
	}
	
	.setBox{
		width: 100%;
		height: 100%;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
		display: flex;
		align-items: center;
		justify-content: center;
		.bj{
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			top: 0;
			background: rgba(0, 0, 0, .2);
		}
		.box{
			position: absolute;
			width: 90%;
			height: auto;
			bottom: 20%;
			.topList{
				width: 100%;
				.list{
					width: 100%;
					height: 100rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: #fff;
					margin-bottom: 1px;
					font-size: 32rpx;
				}
				.list:nth-child(1){
					border-radius: 8px 8px 0 0;
				}
				.list:nth-last-child(1){
					border-radius: 0px 0px 8px 8px;
				}
			}
			.quxiao{
				width: 100%;
				height: 100rpx;
				border-radius: 8px;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 34rpx;
				color: #3478f6;
				font-weight: bold;
				background: #fff;
				margin-top: 30rpx;
			}
		}
	}
</style>